<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>审核订单</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black" />
    <meta name="format-detection" content="telephone=no">
    <meta name="renderer" content="webkit" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="renderer" content="webkit|ie-comp|ie-stand" />
    <link rel="shortcut icon" href="/favicon.ico" type="/image/x-icon">
    <link rel="apple-touch-icon-precomposed" href="">
    <link rel="stylesheet" href="../source/src/css/main.css"/>
    
</head>
<body class="my-order-body">
<!-- 我的订单 -->
<div class="my-order audit-order">
    <div class="order-list">
        <ul class="order-list-ul clearfix" id="auditOrder-list-ul">
                
        </ul>
    </div>
</div>

<!--底部菜单-->
<div class="pub-nav">
    <ul>
        <li>
            <a href="../index.html">
                <div class="index pic"></div>
                <div class="txt">首页</div>
            </a>
        </li>
        <li>
            <a href="./shop_cart.html?v=210818">
                <div class="shopCart pic"></div>
                <div class="txt">购物车</div>
            </a>
        </li>
        <li class="on">
            <a href="./me.html">
                <div class="me pic"></div>
                <div class="txt">我</div>
            </a>
        </li>
    </ul>
</div>
<!--/底部菜单-->

<!--遮罩层-->
<div id="mask" class="hide"></div>

<div class="modal hide">
    <div class="cont-box">
        <div class="hd"></div>
        <div class="bd">
            是否加急提前？
        </div>
        <div class="ft clearfix">
            <a class="sure" id="sure-btn">
                是
            </a>
            <a class="cancel" id="cancel-btn">
                否
            </a>
        </div>
    </div>
</div>


<script id="auditOrder-list-ul-tpl" type="text/html">
    {{each OrderLists as li i}}
    <li class="order-detail {{if li.State != 0}}audited{{/if}} " >
        <div class="hd diy-box">
            <div class="txt flex-1">
                <p class="order-num">
                    订单号：{{li.BillNo}}
                </p>
                <p class="audit-user" id="audit-user">
                    客　户：{{li.CustNo}}
                </p>
            </div>
            {{if li.State == 0}}
            <div class="btn-box">
                <a href="javascript:;" class="delete delete-order" title="删除" data-billNo="{{li.BillNo}}"></a>
            </div>
            {{/if}}
        </div>
        <div class="bd">
            {{if li.ItemList}}
            {{each li.ItemList as item k}}
            <div class="order-item clearfix diy-box" data-item-index="{{k}}" data-index="{{item.Index}}" data-code="{{item.Code}}" data-billNo="{{li.BillNo}}">
                <div class="pro-inro flex-1 diy-box clearfix">
                    <div class="pic">
                        <img src="{{item.Picture}}" alt="{{item.Name}}">
                    </div>
                    <div class="txt flex-1">
                        <h2>{{item.Name}}</h2>
                        <h2>{{item.NameEn}}</h2>
                        <h2>{{item.HelpCode}}</h2>
                        <div class="other">
                            规格：{{item.Model}}
                        </div>

                        {{if li.State == 0}}
                        <div class="order-num-box clearfix">
                            <!--数量-->
                            <div class="change-num clearfix fr">
                                <div class="num_wrap clearfix fl" >
                                    <i class="minus minus_disabled fl" data-initNum="{{item.Qty}}"></i>
                                    <input type="tel" value="{{item.Qty}}"  class="num buynum fl">
                                    <i class="plus fl" data-initNum="{{item.Qty}}"></i>
                                </div>
                            </div>
                            <!--/数量-->
                        </div>
                        {{/if}}

                        {{if item.LotList}}
                        <div class="pici-info">
                            <div class="pici-item clearfix">
                                <div class="col">
                                    有效期
                                </div>
                                <div class="col text-c">
                                    库存
                                </div>
                            </div>
                            {{each item.LotList as lot l}}
                            <div class="pici-item clearfix">
                                <div class="col">
                                    {{lot.KFEDate}}
                                </div>
                                <div class="col text-c">
                                    {{lot.Qty}}
                                </div>
                            </div>
                            {{/each}}
                        </div>
                        {{/if}}
                    </div>
                </div>
                {{if li.State == 0 && li.ItemList && li.ItemList.length>1}}
                <div class="delete-box">
                    <a href="javascript:;" class="delete delete-child-order" title="删除" data-billNo="{{li.BillNo}}"></a>
                </div>
                {{/if}}
            </div>
            {{/each}}
            {{/if}}
        </div>
        <div class="ft clearfix">
            <span class="order-time fl">
                订单日期：{{li.Date}}
            </span>

            {{if li.State == 0}}
            <span class="fr">
                <a href="javascript:;" class="saveInfo-btn" data-billNo="{{li.BillNo}}">
                    保存
                </a>
                <!--<a href="javascript:;" class="audit-btn" data-billNo="{{li.BillNo}}">
                    审核
                </a>-->
            </span>
            {{/if}}
            <!--{{if li.State !=0}}
            <a href="javascript:;" class="audited-btn fr">
                已审核
            </a>
            {{/if}}-->
        </div>
    </li>
    {{/each}}
</script>

<!--审核订单-->
<script src="../source/plugin/zepto.min.js"></script>
<script src="../source/plugin/artTemplate.js"></script>
<script src="../source/src/js/common.js"></script>
<script>
    var init={
        render:function(){

            //render渲染
            var inJson = {
                "custcode":getCookie('CustCode'),
                "empcode":getCookie('EmpCode'),
                "pageindex":1,
                "pagecount":5,
                "key":user.key,
                "account":user.account
            };

            $.ajax({
                url:apiUrl+'/GetOrderItems',
                type:'GET',
                data:{"json":JSON.stringify(inJson)},
                //          dataType :'jsonp',
                success:function(datas){
                    console.log(datas);
                    var html = template('auditOrder-list-ul-tpl', datas);
                    $('#auditOrder-list-ul').html(html);
                },
                error:function(datas){
                    console.log(datas)
                }
            });
        },
        changeSaveBtnJson:function($this,type){
            var itemIndex = $this.parents('.order-item').attr('data-item-index');
            var itemLen = $this.parents('.bd').find('.order-item').length;
            var saveBtn =  $this.parents('li').find('.saveInfo-btn');
//            $this.parents('li').find('.audit-btn').hide();
//            saveBtn.css('display','inline-block');
            var updateJson = {
                "Code": $this.parents('.order-item').attr('data-code'),
                "Name": "",
                "Model": null,
                "Picture": "",
                "Qty": $this.parents('.order-item').find('.buynum').val(),
                "Price": "0",
                "Total": "0",
                "Index": $this.parents('.order-item').attr('data-index'),
                "Type": type
            };

            var inJson = {};
            if(saveBtn.attr('data-json')){
                inJson = JSON.parse(saveBtn.attr('data-json'));
            }else{
                inJson = {
                    "OrderLists": [
                        {
                            "ItemList": [],
                            "CustNo": getCookie('CustCode'),
                            "BillNo": $this.parents('.order-item').attr('data-billNo'),
                            "username":decodeURIComponent(getCookie('username')),
                            "key":user.key,
                            "account":user.account
                        }
                    ]
                };
                for(var i=0;i<itemLen;i++){
                    inJson.OrderLists[0].ItemList[i]={};
                }
            }

            inJson.OrderLists[0].ItemList[itemIndex]=updateJson;
            console.log(inJson);
            saveBtn.attr('data-json',JSON.stringify(inJson));
        },
        addNum:function(){
            var _this = this;
            $('#auditOrder-list-ul').on('tap','.plus',function(){
                var $this = $(this);
                var numObj = $this.siblings('.num');
                var numVal = numObj.val()*1;
                numVal++;
                if(numVal>1){
                    $this.siblings('.minus').removeClass('minus_disabled');
                }
                /* if(numVal>=100){
                 numVal=100;
                 }*/
                numObj.val(numVal);

                //显示保存按钮
                _this.changeSaveBtnJson($this,1);
            });
        },
        reduceNum:function(){
            var _this = this;
            $('#auditOrder-list-ul').on('tap','.minus',function(){
                var $this = $(this);
                var numObj = $this.siblings('.num');
                var numVal = numObj.val()*1;
                numVal--;
                if(numVal<=1){
                    numVal=1;
                    $this.addClass('minus_disabled');
                }
                numObj.val(numVal);

                //显示保存按钮
                _this.changeSaveBtnJson($this,1);

            });
        },
        limitInputTxt:function(){
            var _this = this;
            $('#auditOrder-list-ul').on('input onpropertychange','.buynum',function(){
                var $this = $(this),
                        val = $this.val();
                if(/\D/g.test(val)){
                    $this.val(val.replace(/\D/g,''));
                }

                //显示保存按钮
                _this.changeSaveBtnJson($this,1);
            })
        },
        deleteOrder:function(){
            $('#auditOrder-list-ul').on('click','.delete-order',function(){
                var $this = $(this);
                var inJson = {
                    "BillNo":$this.attr('data-billNo'),
                    "key":user.key,
                    "account":user.account
                };

                $.ajax({
                    url:apiUrl+'/DeleteOrder',
                    type:'GET',
                    data:{"json":JSON.stringify(inJson)},
                    //          dataType :'jsonp',
                    success:function(datas){
                        if(datas.Success==true){
                            $this.parents('li').remove();
                            show_error_tip({str:"订单删除成功"});
                        }else{
                            show_error_tip({str:"订单删除失败"});
                        }
                    },
                    error:function(datas){
                        console.log(datas.msg)
                    }
                });
            });
        },
        deleteChildOrder:function(){
            var _this = this;
            $('#auditOrder-list-ul').on('click','.delete',function(){
                var $this = $(this);

                //显示保存按钮
                _this.changeSaveBtnJson($this,0);
                $this.parents('.order-item').remove();
            });
        },
        updateOrder:function(){
            $('#auditOrder-list-ul').on('click','.saveInfo-btn',function(){
                var $this = $(this);
                var inJson = JSON.parse($this.attr('data-json'));
                $.ajax({
                    url:apiUrl+'/UpdateOrderItems',
                    type:'GET',
                    data:{"json":JSON.stringify(inJson)},
                    //          dataType :'jsonp',
                    success:function(datas){
                        if(datas.Success==true){
//                            $this.parents('.order-item').remove();
                            show_error_tip({str:"保存成功"});
                            //显示审核按钮
//                            $this.parents('li').find('.audit-btn').css('display','inline-block').siblings('.saveInfo-btn').hide();
                        }else{
                            show_error_tip({str:"保存失败"});
                        }
                    },
                    error:function(datas){
                        console.log(datas.msg)
                    }
                });
            });
        },
        auditOrder:function(){
            $('#auditOrder-list-ul').on('click','.audit-btn',function(){
                var $this=$(this);
                $('.modal,#mask').show();
                $('body,html').css({'height':$(window).height(),'overflowY':'hidden'});

                var inJson = {
                    "BillNo":$this.attr('data-billNo'),
                    "account":user.account,
                    "key":user.key,
                    "isUrgent":0
                };
						 		
						 		
                function SubmitOrderCheck(inJson){
                    $.ajax({
                        url:apiUrl+'/SubmitOrderCheck',
                        type:'GET',
                        data:{"json":JSON.stringify(inJson)},
                        //dataType :'jsonp',
                        success:function(datas){
                            if(datas.Success==true){
                                //alert("审核成功");
                                show_error_tip({str:"审核成功"});
                                $this.addClass('audited-btn').removeClass('audit-btn').parents('li.order-detail').addClass('audited');
                            }else{
                                show_error_tip({str:"审核失败"});
                            }
                        },
                        error:function(datas){
                            console.log(datas)
                        }
                    });
                }

                $('#cancel-btn').on('click',function(){
                    $('.modal,#mask').hide();
                    $('body,html').css({'height':'auto','overflowY':'auto'});
                    SubmitOrderCheck(inJson);
                });
                $('#sure-btn').on('click',function(){
                    inJson['isUrgent']=1;
                    $('.modal,#mask').hide();
                    $('body,html').css({'height':'auto','overflowY':'auto'});
                    SubmitOrderCheck(inJson)
                });
            });
        },
        showMorePage:function(){
            window.onload=function(aaa){
                var scrollTimer = null;
                var listUl = $('#list-ul');
                $(window).scroll(function(){
                    clearTimeout(scrollTimer);
                    function showMore(){
                        if($('body,html').height() - $(window).scrollTop() -$(window).height() <= 160){
                            var currentPage = listUl.attr('data-page');
                            var totalPage = Math.ceil(1000/6);
                            currentPage++;
                            if(currentPage<=totalPage){
                                //render渲染
                                var inJson = {
                                    "custcode":getCookie('CustCode'),
                                    "empcode":getCookie('EmpCode'),
                                    "pageindex":currentPage,
                                    "pagecount":5,
                                    "key":user.key,
                                    "account":user.account
                                };

                                $.ajax({
                                    url:apiUrl+'/GetOrderItems',
                                    type:'GET',
                                    data:{"json":JSON.stringify(inJson)},
                                    //          dataType :'jsonp',
                                    success:function(datas){
                                        console.log(datas);
                                        var html = template('auditOrder-list-ul-tpl', datas);
                                        $('#auditOrder-list-ul').append(html);
                                    },
                                    error:function(datas){
                                        console.log(datas)
                                    }
                                });
                            }else{
                                return false;
                            }
                        }
                    }

                    scrollTimer = setTimeout(function(){
                        showMore();
                    },200)
                });
            }
        },
        init:function(){
            this.render();
            this.addNum();
            this.reduceNum();
            this.limitInputTxt();
            this.deleteOrder();
            this.deleteChildOrder();
            this.updateOrder();
            this.auditOrder();
            this.showMorePage();
        }
    };
    init.init();
</script>



</body>
</html>